<!DOCTYPE html>
<html class="x-admin-sm">
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.1</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="__ADMIN__/css/font.css">
    <link rel="stylesheet" href="__ADMIN__/css/xadmin.css">
    <script type="text/javascript" src="__ADMIN__/js/jquery.min.js"></script>
    <script type="text/javascript" src="__ADMIN__/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="__ADMIN__/js/xadmin.js"></script>
    <script type="text/javascript" src="__ADMIN__/js/cookie.js"></script>

  </head>
  <body>
    <div class="x-body">
        <form class="layui-form">
          <input type="hidden" name="id" value={$id}>
          {if condition="empty($specRes)"}
          <div id="attr-div">
            <div class="layui-form-item">
              <label class="layui-form-label" style="padding-top:5px;"><a href="#" onclick="addrow(this)">[+]</a>&nbsp;&nbsp;</label>
              {volist name="attrRes" id="attrRes"}
              <label style="height: 30px;line-height: 30px;margin-right: 5px;float: left;">{$key}</label>
              <div class="layui-input-inline">
                <select name="goods_attr[{$key}][]">
                  <option value="">-请选择-</option>
                  {volist name="attrRes" id="attr"}
                    <option value="{$attr.id}">{$attr.attr_value}</option>
                  {/volist}
                </select>
              </div>
              {/volist}
              <div class="layui-input-inline" style="width:8%;">
                <input type="text" name="inventory[]" id="inventory" placeholder="库存" autocomplete="off" value="" class="layui-input">
              </div>
              <div class="layui-input-inline" style="width:8%;">
                <input type="text" name="price[]" id="price" placeholder="单价" autocomplete="off" value="" class="layui-input">
              </div>
            </div>
          </div>
          {else/}
          <div id="attr-div">
            {volist name="$specRes" id="spec" key="k"}
            <div class="layui-form-item">
              <label class="layui-form-label" style="padding-top:5px;"><a href="#" onclick="addrow(this)">[{if condition="$k eq 1"}+{else /}-{/if}]</a>&nbsp;&nbsp;</label>
              {volist name="$attrRes" id="attrs"}
              <label style="height: 30px;line-height: 30px;margin-right: 5px;float: left;">{$key}</label>
              <div class="layui-input-inline">
                <select name="goods_attr[{$key}][]">
                  {volist name="$attrs" id="attr"}
                    <option {if condition="in_array($attr['id'],$spec['sku'])"}selected="selected"{/if} value="{$attr.id}">{$attr.attr_value}</option>
                  {/volist}
                </select>
              </div>
              {/volist}
              <div class="layui-input-inline" style="width:8%;">
                <input type="text" name="inventory[]" id="inventory" placeholder="库存" autocomplete="off" value="{$spec.inventory}" class="layui-input">
              </div>
              <div class="layui-input-inline" style="width:8%;">
                <input type="text" name="price[]" id="price" placeholder="单价" autocomplete="off" value="{$spec.price}" class="layui-input">
              </div>
            </div>
            {/volist}
          </div>
          {/if}
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  保存
              </button>
          </div>
      </form>
    </div>
    <script>

        layui.use(['form','layer'], function(){
          $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;

          //监听提交
          form.on('submit(add)', function(data){
            $.ajax({
                type:'POST',
                dataType:'json',
                data:data.field,
                dataType:'json',
                url:"{:url('Goods/goods_spec')}",
                success:function(obj){
                  layer.alert(obj.msg, {icon: obj.icon},function(){
                    // 获得frame索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                    // 可以对父窗口进行刷新 
                    x_admin_father_reload();
                  });
                }
            });
            return false;
          });
          
        });
        //增加或减少一行
        function addrow(o){
          var div=$(o).parent().parent();
          if($(o).html()=='[+]'){
              var newdiv=div.clone();
              newdiv.find('a').html('[-]');
              newdiv.find('#inventory').val('');
              newdiv.find('#price').val('');
              div.parent().append(newdiv);
              layui.use(['form'], function(){
                var form = layui.form;
                form.render('select');
              });
          }else{
              div.remove();
              layui.use(['form'], function(){
                var form = layui.form;
                form.render('select');
              });
          }
        }
    </script>
  </body>

</html>